package utilities.utilities_components;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import org.zkoss.zul.Button;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Separator;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Window;

import java.util.Date;
import java.util.Calendar;

import utilities.Updatable;

public class SumUp extends Window{
	private Spinner year;
	private Spinner month;
	
	private Hbox hbox;
	private Calendar calendar;
	
	private static final Date TODAY = new Date();
	
	public SumUp(){
		calendar = Calendar.getInstance();
		
		hbox = new Hbox();
		
		prepareYearSpinner();
		prepareMonthSpinner();
		
		hbox.appendChild( new Label( "统计" ) );
		hbox.appendChild( year );
		hbox.appendChild( new Label( "年" ) );
		hbox.appendChild( month );
		hbox.appendChild( new Label( "月" ) );
		
		/*  Build up separator  */
		Separator sp = new Separator();
		sp.setOrient( "vertical" );
		
		hbox.appendChild( sp );
		
		this.appendChild( hbox );
	}
	
	private void prepareYearSpinner(){
		year = new Spinner( TODAY.getYear() + 1900 );
		
		year.setWidth( "30pt" );
		
		/*  Set constraint  */
		year.setConstraint( "min 1990 max " + ( TODAY.getYear() + 1900 ) );
	}
	
	private void prepareMonthSpinner(){
		month = new Spinner( TODAY.getMonth() + 1 );
		
		month.setWidth( "15pt" );
		
		/*  Set constraint  */
		month.setConstraint( "min 1 max 12" );
	}
	
	
	/*  To be implement  */
	private boolean checkDateValidation(){
		if( ( year.getValue() > TODAY.getYear() ) || ( ( year.getValue() == TODAY.getYear() ) && ( month.getValue() > TODAY.getMonth() ) ) ){
			try{
				new Messagebox().show( "请输入正确的时间!" );
			}catch( InterruptedException exception ){
				/*  Ignore  */
			}
			return false;
		}else
			return true;
	}
	
	public Calendar getDate(){
		Calendar calendar = Calendar.getInstance();
		
		calendar.set( Calendar.YEAR, year.getValue() );
		calendar.set( Calendar.MONTH, month.getValue() );
		
		return calendar;
	}
}
